package edu.gatech.gitmad.catbug.formation;

import java.util.Random;

public abstract class SessionData {
	public static final String databaseurl = "http://www.catbug.net78.net";
	private static String groupID;
	private static String memberID;
	private static String memberName;
	
	private static void generateMemberID() {
		Random rand = new Random();
		memberID = String.format("%15d", rand.nextInt());
	}
	
	public static String getMemberID() {
		if (memberID == null) {
			generateMemberID();
		}
		return memberID;
	}
	
	public static String getGroupID() {
		return groupID;
	}
	
	public static String getMemberName() {
		return memberName;
	}
	
	public static void setGroupID(String id) {
		if (id != null) {
			groupID = id;
		}
	}
	
	public static void setMemberName(String name) {
		if (name != null) {
			memberName = name;
		}
	}
	
	public static void disconnect() {
		HTTPGet get = new HTTPGet(String.format("%s/disconnect.php?MemberID=%s", databaseurl, getMemberID()));
		get.execute();
	}
	
	public static boolean connect() {
		if (memberID != null) {
			disconnect();
		}
		int count = 0;
		String html;
		do {		
			generateMemberID();
			HTTPGet get = new HTTPGet(String.format("%s/connect.php?MemberID=%s", databaseurl, memberID));
			html = get.getHTML();
			count++;
			if (count > 10) {
				return false;
			}
		} while (html.substring(0,1).equals("F"));
		return true;
	}
}
